var express = require("express"),
    bodyParser = require("body-parser"),
    app = express(),
    data = require("./static");

// need it...
app.use( bodyParser.urlencoded({ extended: false }) );

app.get('/', function (req, res) {
  res.send('Hello World!');
});

app.get("/userlist", function(req, res){
  // var str = "<div class='box'>";
  // classDetail.forEach(function(v){
  //   str += "<h2>" + v.majorName + "</h2>";
  // });
  // str += "</div>";

  res.send( JSON.stringify(data.class) );
  // res.json( info );
});

app.get("/user", function(req, res){
  var id = req.query.id;
  var arr = data.users.data.filter(function(v){
    if(v.usrId == id) return v;
  });
  res.json(arr);
});

app.post("/login", function(req, res){
  res.setHeader("Access-Control-Allow-Origin", "*");

  var username = req.body.username;
  var password = req.body.password;

  var arr = data.users.data.filter(function(v){
    if(v.mobile == username && v.pwd == password) return v;
  });

  var result = {};

  if(arr.length > 0){
    result.code = 1;
    result.msg = "登陆成功";
  }else{
    result.code = 0;
    result.msg = "用户名不存在或密码错误";
  }

  res.json(result);
});

var port = 9998;
app.listen(port, function () {
  console.log('服务已启动，端口号' + port);
});
